﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.Infrastructure.Entities.Enum;
using Empires.Infrastructure.Entities.Repository;
using Empires.Infrastructure.Entities.EntityCache;

namespace Empires.Infrastructure.Entities.Entity
{
    public class User : IAccessible<T_User, User>, ILogicEntity<T_User, User>
    {
        #region
        public string UserName { get; set; }
        public string NickName { get; set; }
        public string Password { get; set; }
        public Gender Gender { get; set; }
        public DateTime Birthday { get; set; }
        public string QQ { get; set; }
        public string Phone { get; set; }
        public DateTime RcdDate { get; set; }
        public bool IsRefuse { get; set; }
        public bool IsActivate { get; set; }
        public string Token { get; set; }
        #endregion

        protected override void Covariant(T_User model)
        {
            this.Birthday = model.Birthday;
            this.Gender = model.Gender ? Enum.Gender.Male : Enum.Gender.Female;
            this.IsActivate = model.IsActivate;
            this.IsRefuse = model.IsRefuse;
            this.NickName = model.NickName;
            this.Password = model.Password;
            this.Phone = model.Phone;
            this.QQ = model.QQ;
            this.RcdDate = model.RcdDate;
            this.Token = model.Token;
            this.UserName = model.UserName;
        }
        protected override T_User Inverter(User model)
        {
            return new T_User()
            {
                Birthday = model.Birthday,
                Gender = model.Gender == Enum.Gender.Male,
                IsActivate = model.IsActivate,
                IsRefuse = model.IsRefuse,
                NickName = model.NickName,
                Password = model.Password,
                Phone = model.Phone,
                QQ = model.QQ,
                RcdDate = model.RcdDate,
                Token = model.Token,
                UserName = model.UserName,
            };
        }
        public User Set(T_User dbModel)
        {
            Covariant(dbModel);
            return this;
        }

        /// <summary>
        /// 将启用缓存,通过username查找
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override User Find(string key)
        {
            return new UserCache().GetItem(key);
        }
        public override User PostDelete()
        {
            new UserCache().RefreshItem(UserName);
            return base.PostDelete();
        }
        public override User PostModify()
        {
            new UserCache().RefreshItem(UserName);
            return base.PostModify();
        }
    }
}
